// 根据显示器分辨率，计算数据表格的合适的默认每页条数（原则是刚好占满）
let pageSize = Math.floor((window.screen.height - 440) / 53);

const defaultConfigValues = {
  username: '',
  password: '',
  autoLoginEnabled: false,
  rememberPasswordEnabled: false,
  navMenuExpanded: true,
  autoOpenStatEnabled: false,
  timeShow: true,
  datatable: {
    stripe: true,
    pagination: {
      position: 'right',
      layout: 'total,prev,pager,next,jumper,sizes'.split(','),
      pagerCount: 2,
      background: true,
      pageSize
    },
    timingForce: false
  }
};

class Config {
  constructor() {
    this.initDefaults();
  }

  initDefaults() {
    for (let key in defaultConfigValues) {
      if (this.get(key) == null) {
        this.set(key, defaultConfigValues[key]);
      }
    }
  }

  get(key) {
    return JSON.parse(localStorage.getItem(key));
  }

  /*
  @param keyExpr 配置项属性，如 'theme', 'x.y.z'
  @param value   属性值
  */
  set(keyExpr, value) {
    let path = keyExpr.split('.');
    const key = path[0];
    let rootValues;

    if (path.length > 1) {
      rootValues = config.get(path.shift());
      let values = rootValues;
      while (path.length > 1) {
        values = values[path.shift()];
      }
      values[path[0]] = value;
    } else {
      rootValues = value;
    }
    
    localStorage.setItem(key, JSON.stringify(rootValues));
  }

  getAllValues() {
    let values = {};
    for (let key in defaultConfigValues) {
      values[key] = this.get(key);
    }
    return values;
  }

  restore() {
    localStorage.clear();
    this.initDefaults();
  }
}

const config = new Config();
export default config;

const APP_NAME = '呼贝智慧养老服务平台';
export {
  APP_NAME
};